package cc.openkit.admin.controller.type;

import cc.openkit.admin.model.GGroupLimit;
import cc.openkit.admin.model.ShopType;
import cc.openkit.admin.service.g.GGroupLimitService;
import cc.openkit.admin.service.shopType.ShopTypeService;
import cc.openkit.admin.util.StaticFinalVar;
import cc.openkit.common.KitUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
@RequestMapping("/shopType")
public class ShopTypeController {
    private Logger log = Logger.getLogger(this.getClass());
    @Resource
    private ShopTypeService shopTypeService;
    @Resource
    private GGroupLimitService gGroupLimitService;


    /**
     * 跳转list页面
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public ModelAndView getAll(HttpServletRequest request){

        // 权限验证。这里的最后的 5 就是上面我们找到菜单的ID，需要在这里查找
        GGroupLimit gGroupLimit = gGroupLimitService.testGroup(request, 30);
        List<ShopType> shopTypes = shopTypeService.queryAll();

        ModelAndView mv = new ModelAndView();

        // 没有权限，返回错误页面
        if(gGroupLimit==null){
            mv.setViewName("/admin/err/no_group_err");
            mv.addObject("msg", StaticFinalVar.NO_GROUP_MSG+ StaticFinalVar.CALL_GROUP);
            return mv;
        }

        // 取值
        mv.setViewName("/admin/shopType/list"); // 需要跳转的页面
        mv.addObject("kitG",gGroupLimit);
        mv.addObject("kitList",shopTypes);
        return mv;
    }

    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping(value = "/getAllJson", method = RequestMethod.POST)
    @ResponseBody
    public Object getAllJson(HttpServletRequest request)throws Exception{
        log.info("获取用户列表数据");

        Map<String,Object> map = new HashMap<String,Object>();
        // 分页需要的参数
        String page = request.getParameter("page");// 获得页数
        String limit = request.getParameter("limit");// 获得每页显示条数
        String search = request.getParameter("search");// 获取搜索条件

        // 封装数据
        ShopType shopType = new ShopType();
        if(KitUtil.feikong(search)){
            shopType.setStName(search);
        }

        // 分页查询
        List<ShopType> shopTypes=shopTypeService.queryPageListByWhere(shopType,Integer.valueOf(page), Integer.valueOf(limit));
        int size = shopTypeService.queryCount(shopType);

        // 返回数据
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",shopTypes);

        return JSONObject.toJSON(map);
    }

}
